package my.mall.front.api.util

import my.mall.common.enums.RespMetaEnum
import my.mall.common.exception.WebBaseException
import my.mall.common.util.PasswordUtils.md5
import my.mall.db.entity.User
import org.bouncycastle.cms.RecipientId.password
import org.springframework.util.StringUtils

object UserUtils {
    fun checkUserStatus(user: User?) {
        if (user == null) {
            throw WebBaseException(RespMetaEnum.PARAM_ERROR)
        }
        if (user.userStatus!!.toInt() != 0) {
            throw WebBaseException(RespMetaEnum.NOT_AUTH)
        }
    }

    fun checkUserPassword(targetPwd: String, sourcePwd: String?) {
        var password = targetPwd.trim { it <= ' ' }
        if (!StringUtils.hasLength(password)) {
            throw WebBaseException(RespMetaEnum.PASSWORD_FAILED)
        }
        password = md5(targetPwd)
        if (sourcePwd != password) {
            throw WebBaseException(RespMetaEnum.PASSWORD_FAILED)
        }
    }
}
